package com.yanboo.generator.service.impl;

import com.yanboo.common.annotation.EnableFilter;
import com.yanboo.generator.domain.ProjectInfo;
import com.yanboo.common.utils.ValidationUtil;
import com.yanboo.common.utils.FileUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import com.yanboo.generator.repository.ProjectInfoRepository;
import com.yanboo.generator.service.ProjectInfoService;
import com.yanboo.generator.service.dto.ProjectInfoDto;
import com.yanboo.generator.service.dto.ProjectInfoQueryCriteria;
import com.yanboo.generator.service.mapstruct.ProjectInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.yanboo.common.utils.PageUtil;
import com.yanboo.common.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import com.yanboo.common.utils.PageResult;

/**
* @description 服务实现
* @author fdq
* @date 2024-02-25
**/
@Service
@RequiredArgsConstructor
public class ProjectInfoServiceImpl implements ProjectInfoService {

    private final ProjectInfoRepository projectInfoRepository;
    private final ProjectInfoMapper projectInfoMapper;

    @Override
    @EnableFilter
    @Transactional
    public PageResult<ProjectInfoDto> queryAll(ProjectInfoQueryCriteria criteria, Pageable pageable){
        Page<ProjectInfo> page = projectInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(projectInfoMapper::toDto));
    }

    @Override
    public List<ProjectInfoDto> queryAll(ProjectInfoQueryCriteria criteria){
        return projectInfoMapper.toDto(projectInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ProjectInfoDto findById(Long id) {
        ProjectInfo projectInfo = projectInfoRepository.findById(id).orElseGet(ProjectInfo::new);
        ValidationUtil.isNull(projectInfo.getId(),"ProjectInfo","id",id);
        return projectInfoMapper.toDto(projectInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ProjectInfo resources) {

        projectInfoRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectInfo resources) {
//        ProjectInfo projectInfo = projectInfoRepository.findById(resources.getId()).orElseGet(ProjectInfo::new);
//        ValidationUtil.isNull( projectInfo.getId(),"ProjectInfo","id",resources.getId());
//        projectInfo = projectInfoRepository.findByDbDriverId(resources.getDbDriver().getId());
//        if(projectInfo != null && !projectInfo.getId().equals(projectInfo.getId())){
//            throw new EntityExistException(ProjectInfo.class,"db_driver_id",resources.getDbDriver().getId().toString());
//        }
//        projectInfo1 = projectInfoRepository.findByDbDriverId(resources.getDbDriverId());
//        if(projectInfo1 != null && !projectInfo1.getId().equals(projectInfo.getId())){
//            throw new EntityExistException(ProjectInfo.class,"db_driver_id",resources.getDbDriverId());
//        }
//        projectInfo.copy(resources);
        projectInfoRepository.save(resources);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            projectInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ProjectInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProjectInfoDto projectInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" createBy",  projectInfo.getCreateBy());
            map.put(" createTime",  projectInfo.getCreateTime());
            map.put(" updateBy",  projectInfo.getUpdateBy());
            map.put(" updateTime",  projectInfo.getUpdateTime());
            map.put(" code",  projectInfo.getCode());
            map.put(" dbType",  projectInfo.getDbType());
            map.put(" jdbcUrl",  projectInfo.getJdbcUrl());
            map.put(" name",  projectInfo.getName());
            map.put(" password",  projectInfo.getPassword());
            map.put(" username",  projectInfo.getUsername());
            map.put(" dbDriverId",  projectInfo.getDbDriverId());
            map.put(" createBy",  projectInfo.getCreateBy());
            map.put(" createTime",  projectInfo.getCreateTime());
            map.put(" updateBy",  projectInfo.getUpdateBy());
            map.put(" updateTime",  projectInfo.getUpdateTime());
            map.put(" code",  projectInfo.getCode());
            map.put(" dbType",  projectInfo.getDbType());
            map.put(" jdbcUrl",  projectInfo.getJdbcUrl());
            map.put(" name",  projectInfo.getName());
            map.put(" password",  projectInfo.getPassword());
            map.put(" username",  projectInfo.getUsername());
            map.put(" dbDriverId",  projectInfo.getDbDriverId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}